package com.example.server2;

import com.example.util.DBUtil;
import com.mysql.cj.util.LogUtils;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;

@WebServlet(name = "RegisterServlet", value = "/RegisterServlet")
public class RegisterServlet extends HttpServlet {
    public RegisterServlet() {
        System.out.println("RegisterServlet.RegisterServlet constructor");
    }

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        super.service(req, resp);
        String method = req.getMethod();
        if ("GET".equals(method)){
            doGet(req,resp);
        }else if ("POST".equals(method)){
            doPost(req,resp);
        }else {
            System.out.println("RegisterServlet.service not supported");
        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String account = request.getParameter("account");
        String password = request.getParameter("password");

        System.out.println("RegisterServlet.doGet account "+account);
        System.out.println("RegisterServlet.doGet password "+password);

        String resCode = "";
        String resMsg = "";
        String userId = "";
        Connection connection = DBUtil.getConnect();
        try {
            Statement statement = connection.createStatement();
            ResultSet result;
            String sqlQuery = "select * from " + DBUtil.TABLE_USER_PASSWORD + " where account='" + account + "'";
            result =  statement.executeQuery(sqlQuery);
            if (result.next()){//already has account x
                resCode = "201";
                resMsg = "该账号已注册，请使用此账号直接登录或使用其他账号注册";
                userId = "";
            }else {
                String sqlInsert = "insert into " + DBUtil.TABLE_USER_PASSWORD + "(account,password) values('"+account+"','"+password+"')";
                int rowEffectNums = statement.executeUpdate(sqlInsert);
                if (rowEffectNums == 1){
                    String sqlQueryId = "select id from " + DBUtil.TABLE_USER_PASSWORD + " where account='" + account + "'";
                    ResultSet result2 = statement.executeQuery(sqlQueryId);
                    if (result2.next()){
                        userId = result2.getInt("id")+"";
                    }
                    String sqlInsertInfo = "insert into " + DBUtil.TABLE_USER_INFO + "(id) values('" + userId + "')";
                    int rowEffectNums2 = statement.executeUpdate(sqlInsertInfo);
                    if (rowEffectNums2 == 1){
                        resCode = "100";
                        resMsg = "注册成功";
                    }else{
                        resCode = "203";
                        resMsg = "用户信息表插入错误";
                        userId = "";
                    }
                }else{
                    resCode = "202";
                    resMsg = "用户表插入错误";
                    userId = "";
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
            resCode = "204";
            resMsg = "SQL插入错误 "+e.getMessage();
            userId = "";
        }

        HashMap<String, String> map = new HashMap<>();
        map.put("resCode", resCode);
        map.put("resMsg", resMsg);
        map.put("userId", userId);

        response.setContentType("text/html;charset=utf-8"); // 设置响应报文的编码格式
        PrintWriter pw = response.getWriter(); // 获取 response 的输出流
        pw.println(map.toString()); // 通过输出流把业务逻辑的结果输出
        pw.flush();

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }
}
